import tkinter as tk
import tkinter.font as tf
from tkinter import ttk
import get_data as gd
import draw_picture as dp
import insert_second_tree as ist

def first_window_settings():
    window = tk.Tk()
    window.title("疫情实时数据查询系统")
    window.geometry("650x500")
    #window.resizable(0,0)

    return window


    

def insert_first_window(window):
    data1 = gd.get_data1()
    font = tf.Font(family = "华文仿宋",size=12)
    l1 = tk.Label(window,width=150,height = 1,text = "疫情实时数据查询系统",fg = "white",bg = "black",font = tf.Font(family = "华文隶书",size = 30))
    l1.pack(side=tk.TOP)
    t1=tk.Text(window,width=60,height=1,font=font)
    t1.pack(side=tk.TOP)
    t1.insert("end","统计截至时间："+str(data1['lastUpdateTime'])+"     "+"数据来源：腾讯实时疫情")

    t2=tk.Text(window,width=60,height=1,font=font,foreground="red")
    t2.pack(side=tk.TOP)
    t2.insert("end","全国累计确诊人数："+str(data1['chinaTotal']['confirm'])+"                 "+"全国现存确诊人数："+str(data1['chinaTotal']['nowConfirm']))


    t3=tk.Text(window,width=60,height=1,font=font,foreground="#e8b004")
    t3.pack(side=tk.TOP)
    t3.insert("end","全国疑似病例："+str(data1['chinaTotal']['suspect'])+"                     "+"相较于昨天新增疑似人数："+str(data1['chinaAdd']['suspect']))

    t4=tk.Text(window,width=60,height=1,font=font,foreground="green")
    t4.pack(side=tk.TOP)
    t4.insert("end","全国治愈人数："+str(data1['chinaTotal']['heal'])+"               "+"相较于昨天新增治愈人数："+str(data1['chinaAdd']['heal']))


    t5=tk.Text(window,width=60,height=1,font=font,foreground="gray")
    t5.pack(side=tk.TOP) 
    t5.insert("end","全国死亡人数："+str(data1['chinaTotal']['dead'])+"                    "+"相较于昨天新增死亡人数："+str(data1['chinaAdd']['dead']))

    t6=tk.Text(window,width=45,height=0.6,font=font,foreground="black")
    t6.pack(side=tk.TOP)
    t6.insert("end","   "+"请在下方输入您要查询的省份（或直辖市）")

    e1=tk.Entry(window,width=10)
    e1.pack(side=tk.TOP)
    scrollBar = tk.Scrollbar(window)
    scrollBar.pack(side=tk.RIGHT,fill = tk.Y)
    tree = ttk.Treeview(window,show='headings',yscrollcommand=scrollBar.set)

    b1=tk.Button(window,width=45,text="点击查看该地区详细情况",font = tf.Font(family = "华文仿宋",size=12))
    b1.pack(side=tk.TOP)
    b2 = tk.Button(window,width=45,text = "点击使用其他功能",font = tf.Font(family = "华文仿宋",size=12))
    b2.pack(side=tk.TOP)

    return e1, scrollBar, tree, b1, b2

    
def delForm(tree):
    x=tree.get_children()
    for item in x:
        tree.delete(item)

    
def insert_form(event,m):
    e1 = m[0]
    scrollBar = m[1]
    tree = m[2]

    with open('control.txt',"r") as file_object:
        contents = file_object.read()
    
    if int(contents) == 1:
        delForm(tree)

    data1 = gd.get_data1()['areaTree'][0]['children']
    path = str(e1.get())
    path_data = []
    path_china = []
    for i in data1:
        path_china.append(i['name'])
        path_data.append(i['children'])
    if path in path_china:
        num = path_china.index(path)
        data_path = path_data[num]

        ttk.Treeview.column(tree,"#0",width=100,anchor="center")
        tree["columns"] = ('地区','累计确诊人数','相较于昨日新增确诊人数','累计治愈人数','治愈率','累计死亡人数','死亡率') 
        tree.column('地区', width=100)
        tree.column('累计确诊人数', width=100)
        tree.column('相较于昨日新增确诊人数', width=80)
        tree.column('累计治愈人数', width=100)
        tree.column('治愈率', width=80)
        tree.column('累计死亡人数', width=100)
        tree.column('死亡率', width=80)

        tree.heading('地区', text='地区')
        tree.heading('累计确诊人数', text='累计确诊人数') 
        tree.heading('相较于昨日新增确诊人数', text='今日确诊') 
        tree.heading('累计治愈人数', text='累计治愈人数') 
        tree.heading('治愈率', text='治愈率') 
        tree.heading('累计死亡人数', text='累计死亡人数') 
        tree.heading('死亡率', text='死亡率')
        
        h=0
        for i in data_path:
            name = i['name']
            today = i['today']
            total = i['total']
            tree.insert("",h, text = name,values=(name,str(total['confirm']), str(today['confirm']), str(total['heal']), str(total['healRate']), str(total['dead']), str(total['deadRate'])))
            h+=1

        with open("control.txt",'w') as file_obj:
            file_obj.write("1")
        tree.pack()
        scrollBar.config(command=tree.yview)
    

def handlerAdaptor(fun, **kwds):
    return lambda event,fun=fun,kwds=kwds: fun(event, **kwds)
 
def button_contorl(control_data,window):
    b1 = control_data[3]
    b2 = control_data[4]
    b1.bind("<Button-1>", handlerAdaptor(insert_form, m=control_data))
    b2.bind("<Button-1>", ist.insert_second_window) 
    
    window.mainloop()

